#delimit;



rename *1 *_1; 
rename *2 *_2;
rename *, lower;

egen yearMonthHHid = concat(year month serial);


*>>>> Restrict worker's comp, disability and survivor's benefits to government sources;
gen incgovwkcom=incwkcom if srcwkcom==1;
replace incgovwkcom=0 if srcwkcom!=1;

gen incgovdisab_1=incdisa_1 if srcdisa_1==3 | srcdisa_1==4 | srcdisa_1==5 | srcdisa_1==6 | srcdisa_1==8 | srcdisa_1==9;
replace incgovdisab_1=0 if srcdisa_1==0 |  srcdisa_1==1 | srcdisa_1==2 | srcdisa_1==7 | srcdisa_1==10;

gen incgovdisab_2=incdisa_2 if srcdisa_2==3 | srcdisa_2==4 | srcdisa_2==5 | srcdisa_2==6 | srcdisa_2==8 | srcdisa_2==9;
replace incgovdisab_2=0 if srcdisa_2==0 | srcdisa_2==1 | srcdisa_2==2 | srcdisa_2==7 | srcdisa_2==10;


gen incgovsurv_1=incsurv_1 if srcsurv_1==2 | srcsurv_1==3 | srcsurv_1==4 | srcsurv_1==5;
replace incgovsurv_1=0 if srcsurv_1<=1 | srcsurv_1>=6;
gen incgovsurv_2=incsurv_2 if srcsurv_2==2 | srcsurv_2==3 | srcsurv_2==4 | srcsurv_2==5;
replace incgovsurv_2=0 if srcsurv_2<=1 | srcsurv_2>=6;

 

*>>> Recode missing values;
recode inctot incwage incbus incfarm (. -9999 99999998 9999999=0);
recode incint incdivid incrent incchild incalim incoth incvet incretir incasist incwkcom 
   incdisa*  incsurv* incgovwkcom  incgovdisa*  incgovsurv*  incss incunemp incwelfr incssi 
   inceduc ctc
  (99997 99999 999999=0);
recode eitcred (. 9999=0);



*>>>> Generate household-level variables; 

gen agelt17=1 if (age>=0 & age<=16) ;
replace agelt17=0 if age~=. & age>16;
label var agelt17 "Person is age 0-16" ;

gen agegt64=1 if (age>=65) ;
replace agegt64=0 if (age~=. & age<65); 
label var agegt64 "Person is age 65 or older" ;

destring year, replace;
merge m:1 year using CPI2016.dta, nogen;

sort yearMonthHHid;
foreach var of varlist incwage incbus incfarm incint incdivid incrent incchild incalim
incother incvet incretir incasist eitcred  incgovwkcom incgovdisab* incgovsurv* incss 
incunemp incwelf incssi inceduc ctcc {;
	by yearMonthHHid: egen hh_`var' = total(`var'); 
	*replace hh_`var' = hh_`var'*cpi99 ;
	};


generate pwages_=(incwage+incbus+incfarm) if pernum==1;
generate swages_=(incwage+incbus+incfarm) if (marst>=1 & marst<=3) & pernum==2;
sort  yearMonthHHid;
by yearMonthHHid: egen pwages=max(pwages_);
by yearMonthHHid: egen swages=max(swages_);
replace swages=0 if swages==.;
by yearMonthHHid: egen  agex=total(agegt64);
replace agex=2 if agex>=2;
drop pwages_ swages_;

*>>>> Restrict Working Data-set;
by yearMonthHHid:  egen head=min(pernum); 
keep if pernum==head;

drop if gq==2;
drop if age<25 | age>60;



*>>>> Generate Income Measures;

gen Y = (hh_incwage+hh_incbus+hh_incfarm+hh_incint+hh_incdiv+hh_incrent+hh_incchild+hh_incalim+hh_incoth
  +hh_incvet+hh_incret+hh_incasist+hh_incss);
label var Y "wage/sal + se + int/div + rent + child + alim + oth +vet + assist + retire";


  
gen hh_inceng = heatval; 
gen hh_SNAP = stampval; 

keep if hh_incssi==0 & hh_incgovdisab_1==0 & hh_incgovdisab_2==0; /*20140721 dropping ssi and di recipients */; 
*>>> 20140708 -- Incorporation of children's tax credits, beginning in 2005, and no adjustment for size of household;


gen socialInsurance=hh_incgovwkcom+hh_incunemp; /*20140721 dropped di */;
gen transfers=hh_incwelf+hh_SNAP+hh_inceng+hh_incssi; /*20140721 dropped ssi*/;
gen credit=hh_eitc +hh_ctcc;

gen totalTransfers=transfers+socialInsurance+credit;

drop if Y<0 ;
drop if Y==0 & totalTransfers==0 ;



*>>>> Prep for TAXSIM;


gen state=statefip;
replace state=statefip-1 if statefip>=4 & statefip<7;
replace state=statefip-2 if statefip>7 & statefip<14;
replace state=statefip-3 if statefip>14 & statefip<43;
replace state=statefip-4 if statefip>43 & statefip<52;
replace state=statefip-5 if statefip>52 ;


generate dividend=hh_incdivid;
generate otherprop=(hh_incint+hh_incrent+hh_incchild+hh_incalim+hh_incoth+hh_incvet+hh_incret+hh_incasist);
generate pensions=hh_incretir;
gen ui=hh_incunemp;
gen dividends=hh_incdivid;
rename incssi gssi;
rename marst mstat;
recode mstat (1/3=2) (4/9=1);


gen rentpaid=0;
gen proptax=0;
gen otheritem=0;
gen childcare=0;
gen depx=nchild;

gen depchild=nchild;
gen mortgage=0;
gen stcg=0;
gen ltcg=0;





